<script setup>
import { ref, onMounted } from 'vue'
import postDialog from './components/postDialog.vue'
import { getPostList, deletePostList } from '@/api/post.js'
//获取列表
const list = ref([])
const total = ref(null) //总页数
const form = ref({
  page: 1,
  pageSize: 10,
  positionCode: null,
  postName: '',
  status: null
})
const getPostListApi = async () => {
  const { data } = await getPostList(form.value)
  console.log(data)
  total.value = data.total
  list.value = data.records
}
onMounted(() => {
  getPostListApi()
})
//搜索
const searchBtn = () => {
  form.value.page=1
  getPostListApi()
}
//重置
const resetBtn = () => {
  form.value = {
    page: 1,
    pageSize: form.value.pageSize,
    positionCode: '',
    postName: '',
    status: null
  }
  getPostListApi()
}
//弹框
const showDialog = ref(false)
const addBtn = () => {
  showDialog.value = true
}
//修改
const detaliDialog = ref('')
const editBtn = (id) => {
  detaliDialog.value.getPostDetalisApi(id)
  showDialog.value = true
}
//删除
const delBtn = async (id, status) => {
  if (status === 0) return alert('岗位正在被使用,无法删除')
  await deletePostList(id)
  getPostListApi()
}
//分页
const pageChange = (newPage) => {
  form.value.page = newPage
  getPostListApi()
}
const pageSizeChange = (newPageSize) => {
  form.value.pageSize = newPageSize
  getPostListApi()
}
</script>
<template>
  <div class="container">
    <div class="container_app">
      <div class="searchBox">
        <el-form label-position="top">
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="岗位编码">
                <el-input
                  placeholder="请输入岗位编码"
                  size="large"
                  v-model="form.positionCode"
                ></el-input> </el-form-item
            ></el-col>
            <el-col :span="6">
              <el-form-item label="岗位名称">
                <el-input
                  placeholder="请输入岗位名称"
                  size="large"
                  v-model="form.postName"
                ></el-input> </el-form-item
            ></el-col>
            <el-col :span="6">
              <el-form-item label="状态">
                <el-select
                  placeholder="岗位状态"
                  size="large"
                  style="width: 300px"
                  v-model="form.status"
                >
                  <el-option label="启用" value="0"></el-option>
                  <el-option label="禁用" value="1"></el-option>
                </el-select> </el-form-item
            ></el-col>
            <el-col :span="6" style="margin-top: 29px">
              <el-button type="success" round size="large" style="width: 90px" @click="searchBtn"
                >搜索</el-button
              >
              <el-button round size="large" style="width: 90px" @click="resetBtn">重置</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="btnBox">
        <div style="margin-right: 50px">
          <el-space>
            <el-button type="warning" round size="large" @click="addBtn" style="width: 100px"
              >新增</el-button
            >
          </el-space>
        </div>
      </div>
      <div class="tableBox">
        <el-table stripe :data="list">
          <!-- <el-table-column type="selection"></el-table-column> -->
          <el-table-column label="序号" type="index" width="60">
            <!-- <template #default="{index}">
              {{ (form.page-1)*form.pageSize+index+1 }}
            </template> -->
          </el-table-column>
          <el-table-column label="岗位编码" prop="positionId"></el-table-column>
          <el-table-column label="岗位名称" prop="postName"></el-table-column>
          <el-table-column label="岗位排序" prop="sort"></el-table-column>
          <el-table-column label="状态">
            <template #default="{ row }">
              {{ row.status === 0 ? '启用' : '禁用' }}
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createTime"></el-table-column>
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button type="text" style="color: green" @click="editBtn(row.id)">修改</el-button>
              <el-button type="text" style="color: red" @click="delBtn(row.id, row.status)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="center">
          <el-pagination
            v-model:current-page="form.page"
            v-model:page-size="form.pageSize"
            :page-sizes="[10, 20, 30, 50]"
            :small="small"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @current-change="pageChange"
            @size-change="pageSizeChange"
          />
        </el-row>
      </div>
    </div>
  </div>
  <post-dialog
    ref="detaliDialog"
    v-model:showDialog="showDialog"
    @updateList="getPostListApi()"
  ></post-dialog>
</template>
<style lang="scss" scoped>
.searchBox {
  padding: 20px 45px;
  background-color: #fff;
}
.btnBox {
  display: flex;
  justify-content: end;
  align-items: center;
  height: 90px;
}
.tableBox {
  background-color: #fff;
}
</style>
